//
//  ViewController.swift
//  DynamicStateLJZ
//
//  Created by kys-11 on 2019/4/4.
//  Copyright © 2019年 xl123. All rights reserved.
//

import UIKit
typealias successimageArray = (_ img:[UIImage]) -> ()
typealias successimageStr = (_ img :UIImage) -> ()
class DynamicStateLController: UIViewController,LonginDelegate{
    
    func update() {
        loading()
    }
    

    func JumpPictureDetailsView(vc:ImgCollectionViewController) {
     
        self.present(vc, animated: true, completion: nil)
    }
    
    
    var DYNamicView:DYnamicUIView!
    var ScrollView: UIScrollView!
    var time: Timer!
    override func viewDidLoad() {
        super.viewDidLoad()


        self.title = "讨论"
        self.navigationController?.navigationBar.barTintColor = UIColor(displayP3Red: 78/255, green: 192/255, blue: 255/255, alpha: 1)//设置导航栏背景颜色
        self.navigationController?.navigationBar.tintColor = UIColor.white//设置导航栏上按钮颜色
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]//设置导航栏上标题颜色
        let rightButton = UIBarButtonItem (title: "发布", style: .plain, target: self,
                                           action:#selector(publish))
         //rightButton.image = UIImage(named: "headimage2.jpg")
        
        self.navigationItem.rightBarButtonItem = rightButton
        //自定义左侧按钮取消pop功能
        let leftButton = UIBarButtonItem (title: "关注", style: .plain, target: self,
                                          action:#selector(publishFocus))
//        self.navigationItem.leftBarButtonItem = leftButton
        //加载数据
       loading()
       creat()
        
        
    }
    
    func creat(){
    DYNamicView = DYnamicUIView()
    DYNamicView.loc = self
    DYNamicView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
    self.view.addSubview(DYNamicView)
    }
    
    
    //跳转
    @objc func publish(){
        let vc = ReleaseViewController()
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    @objc func publishFocus(){
//        let vc = FocusOnViewController()
//        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    
    //加载数据
    func loading(){
        BmobAction.Bomrequest(classname: "dyname", success: { (reponse) in
            
            for i in 0..<reponse.count{
                
                print(reponse.count)
                
                let obj = reponse[i] as! BmobObject
                
                //将image数据转换成uiimage类型
                        //将数据传到UIview界面
                self.DYNamicView.update(conten: obj.object(forKey: "content") as! String,timedata: obj.object(forKey: "createdAt") as! String, contentimage: obj.object(forKey: "image") as! [String])
            }
            
        }) { (error) in
            print(error)
        }
        
    }
}

//String 转 图片
func turnPictureArray(imageString:[String],success:@escaping successimageArray){
    var imageArry:[UIImage] = []
    for i in 0 ..< imageString.count{
        let imageStr:String = imageString[i]
        
        if let data: NSData = NSData(base64Encoded: imageStr, options:NSData.Base64DecodingOptions.ignoreUnknownCharacters)
            
        {
            
            if let image: UIImage = UIImage(data: data as Data)
                
            {
                imageArry.append(image)
            }}
    }
    success(imageArry)
}

//String 转 图片
func turnPictureStr(imageString:String,success:@escaping successimageStr){
    
    let imageStr:String = imageString
    
    if let data: NSData = NSData(base64Encoded: imageStr, options:NSData.Base64DecodingOptions.ignoreUnknownCharacters)
        
    {
        
        if let image: UIImage = UIImage(data: data as Data)
            
        {
         success(image)
        }}

}
